<?php
/**
 * Created by PhpStorm.
 * User: Apache
 * Date: 2019/12/5
 * Time: 13:34
 */
namespace app\api\controller\followmanage;
use app\api\controller\common\ApiBase;
use Think\Db;
use app\api\controller\common\CommonPlus;
use app\api\model\Admin as AdminModel;

class Companymaxnumber extends ApiBase
{
    //展商数量列表
    public function index(){
        if (request()->isPost()) {
            $post = input('post.');
                $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
                $keyword = isset($post['keyword']) && $post['keyword'] != '' ? $post['keyword'] : '';
                $page = isset($post['page']) && $post['page'] != '' ? $post['page'] : 1;
                $pagesize = isset($post['pagesize']) && $post['pagesize'] != '' ? $post['pagesize'] : 10;
                $exRes=db('exhibition')->where(['id'=>$exhibition_id])->find();
                if(!$exRes){
                    return json(array('code' => '0', 'msg' => '展会信息错误'));
                }
                $param=" and m.exhibition_id=".$exhibition_id;
                if ($keyword) {
                    $param.=" and m.uid in (select id from crm_admin where realname like '%$keyword%') ";
                }

                $sql2="select m.*,a.realname,e.title";
                $sql2.=" from crm_max_number as m LEFT JOIN crm_admin as a ON a.id=m.uid LEFT JOIN crm_exhibition as e ON e.id=m.exhibition_id where 1=1 $param";
                $sql2 .= " order by m.update_time desc";
                $sql2.=" limit ".($page-1)*$pagesize.",$pagesize";
                $sql21=" select count(m.id) as count1 ";
                $sql21.=" from crm_max_number as m where 1=1 $param";
                $total1=Db::query($sql21);
                $totals=$total1[0]['count1']; //st
                $list=Db::query($sql2);
                return json(['code'=>1,'msg'=>'操作成功','data'=>['list'=>$list,'count'=>$totals]]);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }

    public function ones(){
        if (request()->isPost()) {
            $post = input('post.');
                $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
                $maxNumber = isset($post['maxNumber']) && $post['maxNumber'] != '' ? $post['maxNumber'] : 30;
                $exRes=db('exhibition')->where(['id'=>$exhibition_id])->find();
                if(!$exRes){
                    return json(array('code' => '0', 'msg' => '展会信息错误'));
                }
                $admin = db('admin')->select();
                $error=array();
                $data['exhibition_id']=$exhibition_id;
                $data['create_time']=time();
                $data['update_time']=time();
                foreach ($admin as $v){
                    $data['uid']=$v['id'];
                    $res=db('max_number')->where(['exhibition_id'=>$exhibition_id,'uid'=>$v['id']])->find();
                    if($res){
                        db('max_number')->where(['id'=>$res['id']])->update(['max_number'=>$v['max_number']]);
                    }else{
                        $data['max_number']=$v['max_number'];
                        $insertRes=db('max_number')->insert($data);
                        if(!$insertRes){
                            $error[]=$v['realname'];
                        }
                    }
                }
                if(count($error)>0){
                    return json(['code' => 0, 'msg' => implode('，',$$error).'未添加成功']);
                }
                return json(['code' => 1, 'msg' =>'操作成功']);
                
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    public function add(){
        if (request()->isPost()) {
            $post = input('post.');
                $exhibition_id = isset($post['exhibition_id']) && $post['exhibition_id'] != '' ? $post['exhibition_id'] : 0;
                $uid = isset($post['uid']) && $post['uid'] != '' ? $post['uid'] : 0;
                $maxNumber = isset($post['maxNumber']) && $post['maxNumber'] != '' ? $post['maxNumber'] : 30;
                $uRes=db('max_number')->where(['exhibition_id'=>$exhibition_id,'uid'=>$uid])->find();
                if($uRes){
                    return json(array('code' => '0', 'msg' => '此业务员已添加'));
                }
                $exRes=db('exhibition')->where(['id'=>$exhibition_id])->find();
                if(!$exRes){
                    return json(array('code' => '0', 'msg' => '展会信息错误'));
                }
                $uRes=db('admin')->where(['id'=>$uid])->find();
                if(!$uRes){
                    return json(array('code' => '0', 'msg' => '业务员信息错误'));
                }
                $data['exhibition_id']=$exhibition_id;
                $data['max_number']=$maxNumber;
                $data['uid']=$uid;
                $data['create_time']=time();
                $data['update_time']=time();
                $insertRes=db('max_number')->insert($data);
                if(!$insertRes){
                    return json(['code' => 0, 'msg' => '操作失败']);
                }
                return json(['code' => 1, 'msg' => '操作成功']);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }


    public function edit(){
        if (request()->isPost()) {
            $post = input('post.');
                $ids = isset($post['ids']) && $post['ids'] != '' ? $post['ids'] : '';
                $maxNumber = isset($post['maxNumber']) && $post['maxNumber'] != '' ? $post['maxNumber'] : 30;
                $ids=explode(",", $ids);
                if(count($ids)==0){
                    return json(array('code' => '0', 'msg' => '信息错误'));
                }
                $flag=true;
                $uids='';
                foreach ($ids as $v){
                    $res=db('max_number')->where(['id'=>$v])->find();
                    if(!$res){
                        $flag=false;
                        $uids=$v;
                    }
                }
                if(!$flag){
                    return json(array('code' => '0', 'msg' => $uids.'信息错误'));
                }
                foreach ($ids as $v){
                    db('max_number')->where(['id'=>$v])->update(['max_number'=>$maxNumber,'update_time'=>time()]);
                }
                return json(['code' => 1, 'msg' => '操作成功']);
        } else {
            return json(array('code' => '0', 'msg' => '请求失败'));
        }
    }
}
